package com.aat.dizena.assist.modules;

import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function
 * @info java spring aop
 * @update none
 */
public class CustomPathMatchingFilterChainResolver extends PathMatchingFilterChainResolver {

	private CustomDefaultFilterChainManager customDefaultFilterChainManager;

	public void setCustomDefaultFilterChainManager(CustomDefaultFilterChainManager customDefaultFilterChainManager) {
		this.customDefaultFilterChainManager = customDefaultFilterChainManager;
		setFilterChainManager(customDefaultFilterChainManager);
	}

	public FilterChain getChain(ServletRequest request, ServletResponse response, FilterChain originalChain) {
		FilterChainManager filterChainManager = getFilterChainManager();
		if (!filterChainManager.hasChains()) {
			return null;
		}

		String requestURI = getPathWithinApplication(request);

		List<String> chainNames = new ArrayList<String>();
		// the 'chain names' in this implementation are actually path patterns
		// defined by the user. We just use them
		// as the chain name for the FilterChainManager's requirements
		for (String pathPattern : filterChainManager.getChainNames()) {

			// If the path does match, then pass on to the subclass
			// implementation for specific checks:
			if (pathMatches(pathPattern, requestURI)) {
				chainNames.add(pathPattern);
			}
		}

		if (chainNames.size() == 0) {
			return null;
		}

		return customDefaultFilterChainManager.proxy(originalChain, chainNames);
	}
}
